<?php
class IPLock{
	/**
     * Check IP-Lock
     *
     * @param string $page locked for what?
     * @param string $ip Ip-Adress
     * @param string $length Length in seconds
     * @return bool
     */
    public static function stillLocked($page,$ip,$length) {
        // Delete Old Entries
        $dauerTime = time() - $length;
        DB::getInstance()->query("
        	DELETE FROM 
        		".Tables::$iplockTbl." 
        	WHERE 
        		lockDate <= ".$dauerTime." &&
        		lockPage = '".DB::parseVar($page)."'");
        // Check if still locked
        $check1 = DB::getInstance()->query("
        	SELECT 
        		lockDate 
        	FROM 
        		".Tables::$iplockTbl." 
        	WHERE 
        		lockIp ='".DB::parseVar($ip)."' && 
        		lockPage ='".DB::parseVar($page)."'");
        if (DB::getInstance()->numRows($check1) >= 1) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * Insert IP-Lock
     *
     * @param string $page locked for what?
     * @param string $ip Ip-Adress
     */
    public static function insertIpSperre($page,$ip) {
        $time = time();
        // Entry already exists
        $check1 = DB::getInstance()->query("
        	SELECT 
        		id 
        	FROM 
        		".Tables::$iplockTbl." 
        	WHERE 
        		lockIp ='".DB::parseVar($ip)."' && 
        		lockPage ='".DB::parseVar($page)."'");
        if (DB::getInstance()->numRows($check1) < 1 ) {
            // Neuer Eintrag
            DB::getInstance()->query(
            	"INSERT INTO 
            		".Tables::$iplockTbl."
            	SET 
            		lockPage='".DB::parseVar($page)."',
            		lockIp ='".DB::parseVar($ip)."',
            		lockDate='".$time."'");
        } else {
            // Eintrag erneuern
            DB::getInstance()->query("
            	UPDATE 
            		".Tables::$iplockTbl."  
            	SET 
            		lockDate='".$time."' 
            	WHERE 
            		lockIp='".DB::parseVar($ip)."' &&
            		lockPage = '".DB::parseVar($page)."'");
        }
    }
	
}
?>